﻿Ext.define("PSI.OutPlan.MakePlanForm", {
    createUI: function (entity) {
        var me = this;

        var wnd = Ext.create("Ext.window.Window", {
            title: "下达计划",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 800,
            height: 450,
            layout: "border",
            items: [{
                region: "north",
                border: 0,
                xtype: "panel",
                layout: "fit",
                items: [me.getGrid()],
                height: 350
            }, {
                region: "center",
                id: "editForm",
                xtype: "form",
                layout: "form",
                height: "100%",
                bodyPadding: 5,
                defaultType: 'textfield',
                fieldDefaults: {
                    labelWidth: 60,
                    labelAlign: "right",
                    labelSeparator: "",
                    msgTarget: 'side'
                },
                items: [
                    {
                        xtype: "hidden",
                        name: "idList",
                        value: entity.idList
                    },
                    {
                        id: "editDispatcher",
                        name: "dispatcher",
                        fieldLabel: "调度",
                        allowBlank: false,
                        blankText: "没有输入调度",
                        beforeLabelTextTpl: PSI.Const.REQUIRED,
                        xtype: "psi_bc_dispatcherfield"
                    }
                ],
                buttons: [
                    {
                        text: "确定",
                        formBind: true,
                        iconCls: "psi-button-ok",
                        handler: me.onOK,
                        scope: me
                    },
                    {
                        text: "取消", handler: function () {
                            PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                me.wnd.close();
                            });
                        }, scope: me
                    }
                ]
            }
            ]

        });

        me.wnd = wnd;
    },

    show: function (parentForm, entity, selectedItems) {
        var me = this;

        me.parentForm = parentForm;
        me.selectedItems = selectedItems;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(selectedItems); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (selectedItems) {
        Ext.getCmp("editDispatcher").focus();

        var store = this.getGrid().getStore();

        for (var i = 0; i < selectedItems.length; i++) {
            var item = selectedItems[i].getData();
            store.add({
                id: item.id,
                address: item.address,
                cntNumber: item.cntNumber,
                cntType: item.cntType,
                takeCntDT: item.takeCntDT,
                memo: item.memo,
                vsl: item.vsl,
                clients: item.clients,
                cntOwner: item.cntOwner,
                loadCntMan: item.loadCntMan,
                loadCntManTel: item.loadCntManTel,
                ton: item.ton,
                goods: item.goods,
                destPort: item.destPort
            });
        }
    },

    getGrid: function () {
        var me = this;
        if (me.__grid == null) {
            me.__grid = me.createGrid();
        }

        return me.__grid;
    },

    createGrid: function () {
        var modelName = "PSIOutPlanModel_MakePlan";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "address", "cntNumber", "cntType", "takeCntDT", "memo",
                            "planDT", "dispatcher",
                            "vsl", "clients", "cntOwner",
                            "loadCntMan", "loadCntManTel", "ton", "destPort", "goods"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            columnLines: true,
            columns: [
                    { header: "id", dataIndex: "id", hidden: true },
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "货物", dataIndex: "goods", menuDisabled: true },
                    { header: "装箱时限", dataIndex: "takeCntDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });

    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        el.mask("数据保存中...");
        f.submit({
            url: "/OutPlan/MakePlan",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.wnd.close();
                me.parentForm.refreshPlanGrid();
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editDispatcher").focus();
                });
            }
        });
    }
});